<?php

require_once dirname(__FILE__).'/../lib/customarOrderGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/customarOrderGeneratorHelper.class.php';

/**
 * customarOrder actions.
 *
 * @package    trunk
 * @subpackage customarOrder
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class customarOrderActions extends autoCustomarOrderActions {
    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

            try {

                $customar_order = $form->save();
                if($customar_order->status == 'Complete')
                    CustomarOrderTable::updateGroup($this->getUser()->getUserId(),'Premier');
                
            } catch (Doctrine_Validator_Exception $e) {

                $errorStack = $form->getObject()->getErrorStack();

                $message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ?  's' : null) . " with validation errors: ";
                foreach ($errorStack as $field => $errors) {
                    $message .= "$field (" . implode(", ", $errors) . "), ";
                }
                $message = trim($message, ', ');

                $this->getUser()->setFlash('error', $message);
                return sfView::SUCCESS;
            }

            $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $customar_order)));

            if ($request->hasParameter('_save_and_add')) {
                $this->getUser()->setFlash('notice', $notice.' You can add another one below.');

                $redirection = $this->getRouteArrayForAction('new');
            }
            elseif ($request->hasParameter('_save_and_list')) {
                $this->getUser()->setFlash('notice', $notice);

                $redirection = $this->getRouteArrayForAction('index');
            }
            elseif ($request->hasParameter('_save_and_next')) {
                $this->getUser()->setFlash('notice', $notice);
                $redirection = $this->getRouteArrayForAction('edit', dmArray::get($customar_order->getPrevNextRecords($this->buildQuery()), 'next', $this->form->getObject()));
            }
            else {
                $this->getUser()->setFlash('notice', $notice);

                $redirection = $this->getRouteArrayForAction('edit', $customar_order);
            }

            $this->redirect($redirection);
        }
        else {
            $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
        }
    }

}
